package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.RoleService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.Role;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.RoleMapper;
import com.his.util.constant.RedisConstant;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import com.his.util.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    private RoleMapper roleMapper;
    private RedisTemplate redisTemplate;

    @Override
    public RestResult all(String authorization, ListDto listDto) {
        Integer roleId = StringUtil.getId(RedisConstant.ROLE, (String) redisTemplate.opsForValue().get(RedisConstant.getRoleAndAuthorization(authorization)));
        Role role = MapUtil.conversionMapToObj((LinkedHashMap) listDto.getObj(), Role.class);
        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>()
                .like(role.getRoleName() != null, "role_name", role.getRoleName())
                .ge("role_id", roleId);
        IPage page = roleMapper.selectPage(iPage, queryWrapper);

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(page.getRecords())
                .total(page.getTotal())
                .build());
    }
}
